package com.example.casper.Experiment2024.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.example.casper.Experiment2024.R;

public class Submarine {
    private int x, y;
    private int width, height;
    private int screenWidth, screenHeight;
    private Bitmap submarineBitmap;
    private Context context;

    public Submarine(Context context, int screenWidth, int screenHeight) {
        this.context = context; // 保存上下文
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
        width = 200;
        height = 100;
        x = screenWidth / 2 - width / 2;
        y = screenHeight - height - 50;

        submarineBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.submarine);
        submarineBitmap = Bitmap.createScaledBitmap(submarineBitmap, width, height, false);
    }

    public void update() {
        // 可在此扩展逻辑
    }

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(submarineBitmap, x, y, paint);
    }

    public void handleTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            x = (int) event.getX() - width / 2; // 控制潜艇位置
            y = (int) event.getY() - height / 2;
            // 边界检测
            x = Math.max(0, Math.min(x, screenWidth - width));
            y = Math.max(0, Math.min(y, screenHeight - height));
        }
    }

    public Rect getBounds() {
        return new Rect(x, y, x + width, y + height);
    }
}